package com.gxa.controller.yhy.Patient;


import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.common.vo.TransCondition;
import com.gxa.service.PatientCommonService;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@Api(value = "患者公海操作功能",tags = "患者公海所有操作功能接口API")
@RequiresRoles(value = {"超级管理员","院长","主任","秘书","助理"},logical = Logical.OR)
@RequestMapping("Patient/common")
public class PatientCommonController {

    @Autowired
    PatientCommonService patientCommonService;

    @RequestMapping(value = "/findAllPatientList",method = RequestMethod.GET)
    @ApiOperation(value = "展示所有跟进信息", notes = "获取跟进信息", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每一页页数", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
    /**
     * 显示类型为公海的患者信息
     */
    public R findAllPatientList(Integer currentPage, Integer pageSize) {
        R r = new R<>();
        if (currentPage != null && pageSize != null) {
            PageInfo allPatientList = patientCommonService.getAllPatientList(currentPage, pageSize);
            if (allPatientList != null) {
                r.fromStatus(Status.SUCCESS);
                r.setData(allPatientList);
            } else {
                r.fromStatus(Status.SYSTEM_ERROR);
            }
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }

    @RequestMapping(value = "/assignPatients", method = RequestMethod.POST)
    @ApiOperation(value = "分配所有跟进信息", notes = "获取跟进信息", httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })

    @ResponseBody
    /**
     * 分配患者
     */
    public R assignPatients(@RequestBody @ApiParam(name = "transCondition",value = "批量转交患者信息实体",required = true)TransCondition transCondition) {

        R r = new R<>();

        if (transCondition != null) {

            boolean b = patientCommonService.assignPatients(transCondition);

            if (b == true) {

                r.fromStatus(Status.SUCCESS);

            }else {
                r.fromStatus(Status.SYSTEM_ERROR);
            }
        }else {
            r.fromStatus(Status.BAD_REQUEST_PARAM);
        }
        return r;
    }
    @RequestMapping(value = "/receivePatient", method = RequestMethod.POST)
    @ApiOperation(value = "负责人领取", notes = "负责人领取公海患者", httpMethod = "POST", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "领取成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "patientId", value = "患者id", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
    /**
     *负责人领取公海患者信息
     */
    public R receivePatient(@RequestParam("userId") Integer userId,@RequestParam("patientId") Integer patientId) {

        R r = new R<>();
        if (userId != null && patientId != null) {
            boolean b = patientCommonService.assignPatient(userId, patientId);
            if (b == true) {
                r.fromStatus(Status.SUCCESS);
            }else {
                r.fromStatus(Status.SYSTEM_ERROR);
            }
        }else {
            r.fromStatus(Status.BAD_QUERY_PARAM);
        }
        return r;
    }


    @RequestMapping(value = "/getCommonPatientListByName", method = RequestMethod.GET)
    @ApiOperation(value = "通过查找公海患者信息", notes = "通过查找公海患者信息", httpMethod = "GET", response = R.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "领取成功"),
            @ApiResponse(code = 400, message = "代表参数格式不对"),
            @ApiResponse(code = 404, message = "代表找不到此请求"),
            @ApiResponse(code = 500, message = "代表后端代码出现异常"),
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每一页页数", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "name", value = "患者姓名", paramType = "query", dataType = "String", required = true)
    })
    @ResponseBody
    /**
     *通过NAME查找公海患者信息
     */
    public R getCommonPatientListByName(Integer currentPage,Integer pageSize,String name) {
        R r = new R<>();
        PageInfo commonPatientList = patientCommonService.getCommonPatientList(currentPage, pageSize, name);
        if (commonPatientList != null) {
            r.fromStatus(Status.SUCCESS);
            r.setData(commonPatientList);
        }else {
            r.fromStatus(Status.SYSTEM_ERROR);
        }
        return r;
    }

}
